package cn.ilovese.collect.serviceimpl.report.similaritystrategy;

import cn.ilovese.collect.datamapper.report.ReportMapper;
import cn.ilovese.collect.dto.similarity.MySimHash;
import cn.ilovese.collect.po.report.Report;
import cn.ilovese.collect.service.report.similaritystrategy.TextSimilarityStrategy;
import cn.ilovese.collect.vo.report.ReportFormVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class SimHashStrategy implements TextSimilarityStrategy {
    @Resource
    ReportMapper reportMapper;

    @Override
    public Double CalcSimilarity(ReportFormVO reportVO,Integer taskid) {
        List<Report> reportList = reportMapper.selectByTaskId(taskid);
        String thisText=reportVO.getDescription()+reportVO.getSteps()+reportVO.getEnvironment();
        Double res=0.0;//暂时方案：取最大重复度
        for(Report report: reportList){
            String text=report.getDescription()+report.getSteps()+report.getDeviceInfo();
            res=Math.max(res,TextSimilarity(thisText,text));
        }
        return res;
    }

    @Override
    public Double CalSimilarityOneToOne(Report first, Report second) {
        String firstInfo = first.getDescription() + first.getSteps() + first.getDeviceInfo();
        String secondInfo = second.getDescription() + second.getSteps() + second.getDeviceInfo();
        return TextSimilarity(firstInfo,secondInfo);
    }


    public Double TextSimilarity(String text1,String text2){
        Double res=0.0;
        MySimHash hash1 = new MySimHash(text1, 64);
        MySimHash hash2 = new MySimHash(text2, 64);
        res=hash1.getSemblance(hash2);
        return res;

    }
}
